/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package module1;


import dao.KhachHangDAO;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.KhachHang;

/**
 *
 * @author OSIRIS
 */
public class KhachHangFrame extends javax.swing.JInternalFrame {

    /**
     * Creates new form KhachHangFrame
     */
    LapHoaDonFrame lhdFrame;
    public KhachHangFrame(LapHoaDonFrame lhdFr,String action) {
        initComponents();
        lhdFrame=lhdFr;
        if(action=="menu"){
            jBtnAddHoaDon.setVisible(false);  
        }
        if(action=="addHD"){
            jBtnAddHoaDon.setVisible(true);  
        }
       
    }
    public void load(){
        KhachHangDAO khDao=new KhachHangDAO();
        List ds=khDao.findAll();
         DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("ID");
        dtm.addColumn("Họ Tên");
        dtm.addColumn("Email");
        dtm.addColumn("Địa Chỉ");
        dtm.addColumn("Sdt");
        for (int i = 0; i < ds.size(); i++) {
            KhachHang dm = (KhachHang) ds.get(i);
            Vector vt = new Vector();
            vt.add(dm.getId());
            vt.add(dm.getHoTen());
            vt.add(dm.getEmail());
            vt.add(dm.getDiaChi());
            vt.add(dm.getSdt());
            dtm.addRow(vt);
        }
        this.jTableDsKhachHang.setModel(dtm);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableDsKhachHang = new javax.swing.JTable();
        jBtnThem = new javax.swing.JButton();
        jBtnXoa = new javax.swing.JButton();
        jBtnSua = new javax.swing.JButton();
        jBtnAddHoaDon = new javax.swing.JButton();

        setClosable(true);
        setTitle("Danh Sách Khách Hàng");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jTableDsKhachHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTableDsKhachHang);

        jBtnThem.setText("Thêm ");
        jBtnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnThemActionPerformed(evt);
            }
        });

        jBtnXoa.setText("Xóa");
        jBtnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnXoaActionPerformed(evt);
            }
        });

        jBtnSua.setText("Sửa");
        jBtnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSuaActionPerformed(evt);
            }
        });

        jBtnAddHoaDon.setText("Thêm Vào Hóa Đơn");
        jBtnAddHoaDon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAddHoaDonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 602, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jBtnThem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnXoa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnSua, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(161, 161, 161))
            .addGroup(layout.createSequentialGroup()
                .addGap(270, 270, 270)
                .addComponent(jBtnAddHoaDon)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jBtnAddHoaDon)
                .addGap(0, 113, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addComponent(jBtnThem)
                .addGap(18, 18, 18)
                .addComponent(jBtnXoa)
                .addGap(21, 21, 21)
                .addComponent(jBtnSua)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        load();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jBtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnThemActionPerformed
        // TODO add your handling code here:
        try {
            KhachHangThemDialog dlg = new KhachHangThemDialog(null, true);
            dlg.setTitle("Thêm");
            dlg.setVisible(true);
            KhachHangDAO khDAO = new KhachHangDAO();
            KhachHang info = new KhachHang();
            info=dlg.getInfo();
            khDAO.create(info);
            this.load();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }
    }//GEN-LAST:event_jBtnThemActionPerformed
  private int getIdToTable() {
        int selectedRowIndex = jTableDsKhachHang.getSelectedRow();
        //int selectedColumnIndex = jTable.getSelectedColumn();
        int selectedObject = (Integer) jTableDsKhachHang.getModel().getValueAt(selectedRowIndex, 0);
        return selectedObject;
    }
    private void jBtnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnXoaActionPerformed
        // TODO add your handling code here:
        try {
            KhachHangDAO khDAO = new KhachHangDAO();
            KhachHang info = new KhachHang();
            info.setId(this.getIdToTable());
            khDAO.delete(info);
            this.load();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }
    }//GEN-LAST:event_jBtnXoaActionPerformed

    private void jBtnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSuaActionPerformed
        // TODO add your handling code here:
      
    }//GEN-LAST:event_jBtnSuaActionPerformed

    private void jBtnAddHoaDonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAddHoaDonActionPerformed
        // TODO add your handling code here:
         try {
            KhachHangDAO khDAO = new KhachHangDAO();
          
          KhachHang khach_hang=khDAO.find(this.getIdToTable());
         lhdFrame.AddKhachHang(khach_hang);
           this.dispose();
            
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "error");
        }
    }//GEN-LAST:event_jBtnAddHoaDonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnAddHoaDon;
    private javax.swing.JButton jBtnSua;
    private javax.swing.JButton jBtnThem;
    private javax.swing.JButton jBtnXoa;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableDsKhachHang;
    // End of variables declaration//GEN-END:variables

   
}
